package com.example.hp.storeapp.fragment;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.hp.storeapp.R;
import com.example.hp.storeapp.base.BaseFragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment {
    private TextView titleView;

    private RadioGroup radioGroup;
    private  ShopListFragment shopListFragment;
    private  FavorFragment favorFragment;
    private SearchFragment searchFragment;
    private  MyFragment myFragment;

    private  FragmentManager manager;
    private FragmentTransaction transaction;




    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        titleView = (TextView) view.findViewById(R.id.textView_title);
        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);

        manager = getChildFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.radio_shop_list:
                        shopListFragment=new ShopListFragment();
                        replace(shopListFragment);

//                        shopListFragment = new ShopListFragment();
//                        transaction = manager.beginTransaction();
//                        transaction.replace(R.id.fragmentLayout, shopListFragment);
//                        transaction.commit();


                        break;
                    case  R.id.radio_favor:
                        favorFragment= new FavorFragment();
                        replace(favorFragment);

                        break;
                    case  R.id.radio_search:
                        searchFragment=new SearchFragment();
                        replace(searchFragment);
                        break;
                    case  R.id.radio_my:
                        myFragment=new MyFragment() ;
                        replace(myFragment);
                        break;

                }
            }
        });
        radioGroup.check(R.id.radio_shop_list);

    }
    private void replace(Fragment fragment) {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fragmentLayout, fragment);
        transaction.commit();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

}
